package View.BreakRule;

import Service.BreakRuleService;
import Service.ServiceImpl.BreakRuleServiceImpl;
import View.Index.Index_admin;
import domain.BreakRule;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class addBreakRule extends JFrame {


    public addBreakRule(User user){
        init();
        componentInit(user);
    }
    public void init(){
        this.setTitle("添加违纪记录");
        this.setSize(453,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("学号:");
        label1.setBounds(123,40,65,30);
        JLabel label2= new JLabel();
        label2.setText("姓名:");
        label2.setBounds(123,93,65,30);
        JLabel label3= new JLabel();
        label3.setText("违纪原因:");
        label3.setBounds(123,149,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(210,38,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(211,93,120,30);
        JTextArea field3= new JTextArea();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(212,151,176,70);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("添加");
        button1.setBounds(69,266,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(278,266,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BreakRule rule = new BreakRule();
                rule.setStunum(field1.getText());
                rule.setName(field2.getText());
                rule.setDisReason(field3.getText());
                rule.setWriteTime(new Date());

                BreakRuleService service = new BreakRuleServiceImpl();
                service.addBreakRuleDao(rule);

                JOptionPane.showMessageDialog(new JFrame(),"添加成功！");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
